<?php
/**
 * Created by PhpStorm.
 * User: Yui
 * Date: 2017/5/9 0009
 * Time: 11:06
 */

namespace backend\controllers;

use common\library\comFun;
use common\services\ArticleClassSve;
use Yii;
use common\controllers\BackendCommonCtrl;
use yii\helpers\Url;

class ArticleClassController extends BackendCommonCtrl
{
    public function actionList()
    {
        $ArticleClassSve = new ArticleClassSve();
        $rs = $ArticleClassSve->getList();
        return $this->render('list', $rs['data']);
    }

    /**
     * 添加
     * @return string
     */
    public function actionAdd()
    {
        return $this->render('edit');
    }

    /**
     * 编辑
     * @return string
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;
        $ArticleClassSve = new ArticleClassSve();
        if ($request->isPost) {
            if ($request->post('article_class_id')) {
                $rs = $ArticleClassSve->update($request->post());
            } else {
                $rs = $ArticleClassSve->add($request->post());
            }
            if ($rs['code'] === 0) {
                $rs['redirect'] = Url::toRoute('/article-class/list');
            }
            return $this->asJson($rs);
        } else {
            $id = $request->get('article_class_id');
            $data = [];
            //检查id
            if (comFun::isPositiveInt($id)) {
                $rs = $ArticleClassSve->getInfo(['article_class_id' => $id]);
                if ($rs['code'] === 0) {
                    $data = $rs['data'];
                }
            }
            return $this->render('edit', ['info' => $data['Obj']]);
        }
    }

    /**
     * 移除
     * @return array
     */
    public function actionRemove()
    {
        $articleClassId = Yii::$app->request->get('article_class_id');
        $ArticleClassSve = new ArticleClassSve();
        $rs = $ArticleClassSve->remove(['article_class_id' => $articleClassId]);
        return $this->asJson($rs);
    }
}